package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Filter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/http;charset=UTF-8");
        if (req.getSession().getAttribute("username") == null) {
            resp.sendRedirect("/login.jsp");
        } else {
            filterChain.doFilter(servletRequest,servletResponse);
        }


    }

    @Override
    public void destroy() {

    }
}
